package com.zb.frame.modules.sys.utils;

import com.zb.frame.base.upgrade.common.UpgradeHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UpgradeUtils {

    private  static Logger logger = LoggerFactory.getLogger(UpgradeUtils.class);

    /**
     * 检验表是否存在
     * @param conn
     * @param tableName
     * @return
     */
    public static boolean checkTableExist(Connection conn, String tableName, String tips) {
        String sqlCheck = "select 1 from " + tableName + " where 1!=1";
        boolean flag = UpgradeHelper.checkExist(conn, sqlCheck);
        if(flag)
            logger.debug( tips + " create table【" + tableName + "】 has exist in database.");
        return flag;
    }

    /**
     * 向系统用户表添加数据（用户）
     */
    public static void insertDataUser(Connection conn, String  tableName, String userName, String password, String alias, String phone, String email, String desc, String avatar) {
        PreparedStatement prep = null;
        String sql = "insert into "+ tableName +"(user_name,user_password,user_alias,user_phone,user_email,user_sex,user_desc,user_avatar,user_status,create_user,create_time,update_user,update_time) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            prep = conn.prepareStatement(sql);
            prep.setString(1, userName);
            prep.setString(2, password);
            prep.setString(3, alias);
            prep.setString(4, phone);
            prep.setString(5, email);
            prep.setInt(6, 1);
            prep.setString(7, desc);
            prep.setString(8, avatar);
            prep.setInt(9,1);
            prep.setLong(10,1L);
            Date date = new Date(System.currentTimeMillis());
            prep.setDate(11, date);
            prep.setLong(12, 1L);
            prep.setDate(13, date);
            prep.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**
     *  向表系统角色表添加数据（角色）
     */
    public static void insertDataRole(Connection conn, String tableName, String roleId, String roleName, String roleDesc) {
        PreparedStatement prep = null;
        String sql = "insert into "+ tableName +"(role_id,role_name,role_desc,is_sys,create_user,create_time,update_user,update_time) values(?,?,?,?,?,?,?,?)";
        try {
            prep = conn.prepareStatement(sql);
            prep.setString(1, roleId.toUpperCase());
            prep.setString(2, roleName);
            prep.setString(3, roleDesc);
            prep.setInt(4, 1);
            Date date = new Date(System.currentTimeMillis());
            prep.setLong(5, 1L);
            prep.setDate(6, date);
            prep.setLong(7, 1L);
            prep.setDate(8, date);
            prep.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     *  向表sys_rel_user_role添加数据（用户角色关系）
     */
    public static void insertDataUserRoleRel(Connection conn, String tableName, Long userId, String roleId) {
        PreparedStatement prep = null;
        String sql = "insert into "+ tableName +"(user_id,role_id,create_user,create_time,update_user,update_time) values(?,?,?,?,?,?)";
        try {
            prep = conn.prepareStatement(sql);
            prep.setLong(1, userId);
            prep.setString(2, roleId);
            Date date = new Date(System.currentTimeMillis());
            prep.setLong(3, 1L);
            prep.setDate(4, date);
            prep.setLong(5, 1L);
            prep.setDate(6, date);
            prep.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     *  向表sys_menu添加数据（菜单资源）
     */
    public static void insertDataMenu(Connection conn, String tableName, Long pId, String menuName, String menuAlias, String menuIcon, Integer menuType, String menuPath, String menuPerm, String menuDesc, Integer order) {
        PreparedStatement prep = null;
        String sql = "insert into "+ tableName +"(p_id,menu_name,menu_alias,menu_icon,menu_type,menu_path,menu_perm,menu_desc,menu_order,create_user,create_time,update_user,update_time) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            prep = conn.prepareStatement(sql);
            prep.setLong(1, pId);
            prep.setString(2, menuName);
            prep.setString(3, menuAlias);
            prep.setString(4, menuIcon);
            prep.setInt(5, menuType);
            prep.setString(6, menuPath);
            prep.setString(7, menuPerm);
            prep.setString(8, menuDesc);
            prep.setInt(9, order);
            Date date = new Date(System.currentTimeMillis());
            prep.setLong(10, 1L);
            prep.setDate(11, date);
            prep.setLong(12, 1L);
            prep.setDate(13, date);
            prep.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     *  向表sys_rel_role_menu添加数据（角色菜单关系）
     */
    public static void insertDataMenuRoleRel(Connection conn, String tableName, String roleId, Long MenuId) {
        PreparedStatement prep = null;
        String sql = "insert into "+ tableName +"(role_id,menu_id,create_user,create_time,update_user,update_time) values(?,?,?,?,?,?)";
        try {
            prep = conn.prepareStatement(sql);
            prep.setString(1, roleId);
            prep.setLong(2, MenuId);
            Date date = new Date(System.currentTimeMillis());
            prep.setLong(3, 1L);
            prep.setDate(4, date);
            prep.setLong(5, 1L);
            prep.setDate(6, date);
            prep.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
